ReactNativeを使った開発で、androidでビルドした時にBUG! exception in phase 'semantic analysis' in source unit 'BuildScript' Unsupported class file major version 63と表示されて失敗する場合の対処法をまとめました。

エラー内容

$ npx react-native run-android
warn Multiple Podfiles were found: ios/Podfile,vendor/bundle/ruby/2.7.0/gems/cocoapods-core-1.11.3/lib/cocoapods-core/Podfile. Choosing ios/Podfile automatically. If you would like to select a different one, you can configure it via "project.ios.sourceDir". You can learn more about it here: https://github.com/react-native-community/cli/blob/master/docs/configuration.md
info JS server already running.
info Installing the app...

FAILURE: Build failed with an exception.

* What went wrong:
Could not open settings generic class cache for settings file '~/study/MyApp/android/settings.gradle' (~/.gradle/caches/7.5.1/scripts/77sintqqnf5h3dygr0jrmxhpw).
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 63

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 523ms

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* What went wrong:
Could not open settings generic class cache for settings file '~/study/MyApp/android/settings.gradle' (~/.gradle/caches/7.5.1/scripts/77sintqqnf5h3dygr0jrmxhpw).
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 63

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 523ms

    at makeError (~/study/MyApp/node_modules/execa/index.js:174:9)
    at ~/study/MyApp/node_modules/execa/index.js:278:16
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async runOnAllDevices (~/study/MyApp/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:109:5)
    at async Command.handleAction (~/study/MyApp/node_modules/@react-native-community/cli/build/index.js:142:9

原因

Android Gradle fails with > BUG! exception in phase 'semantic analysis' in source unit 'BuildScript' Unsupported class file major version 62

動かすjavaのバージョンが違うのが原因らしい。 JDKの11を使えとのこと。

調査

$ npx react-native info
info Fetching system and libraries information...
System:
    OS: macOS 13.1
    CPU: (10) arm64 Apple M1 Pro
    Memory: 179.16 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.17.0 - ~/.anyenv/envs/nodenv/versions/16.17.0/bin/node
    Yarn: 1.22.19 - ~/study/MyApp/node_modules/.bin/yarn
    npm: 8.15.0 - ~/.anyenv/envs/nodenv/versions/16.17.0/bin/npm
    Watchman: 2022.10.17.00 - /opt/homebrew/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /opt/homebrew/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 22.2, iOS 16.2, macOS 13.1, tvOS 16.1, watchOS 9.1
    Android SDK: Not Found
  IDEs:
    Android Studio: 2021.3 AI-213.7172.25.2113.9123335
    Xcode: 14.2/14C18 - /usr/bin/xcodebuild
  Languages:
    Java: javac 19 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.1.0 => 18.1.0 
    react-native: 0.70.6 => 0.70.6 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found
[12/23/22 21:16]~/study/MyApp $ java -version
openjdk version "19" 2022-09-20
OpenJDK Runtime Environment Homebrew (build 19)
OpenJDK 64-Bit Server VM Homebrew (build 19, mixed mode, sharing)

JDKのバージョンが違うのでインストールすることにしました。

対策

以下のサイトに沿ってOracke Java 11をインストールします。

macOS に Oracle Java 11 (JDK) をインストールする手順

5 Installation of the JDK on macOS

1. JDKのインストール

Java SE Development Kit 11.0.17

私はmac OSのArm 64 DMG Installerをダウンロードしてインストールしました。 Oracleのアカウントを作成しないとDLできないみたいです。

2. パスの設定

$ vim ~/.zshrc 

.zshrcの中身

export JAVA_HOME=`/usr/libexec/java_home -v 11`
$ source ~/.zshrc

3. javaのバージョンの確認

$ java -version                                     
openjdk version "11.0.17" 2022-10-18 LTS
OpenJDK Runtime Environment Zulu11.60+19-CA (build 11.0.17+8-LTS)
OpenJDK 64-Bit Server VM Zulu11.60+19-CA (build 11.0.17+8-LTS, mixed mode)
$ which java                                        
/usr/bin/java

JDK11がインストールされました。

再ビルド

$ npx react-native run-android

無事ビルドできました!